local ludzie
local zombies
local missionTimer
local humanSkins = {7,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,43,44,45,36,37,38,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,66,67,68,71,72,73,75,76,77,78,79,80,81,82,83,85,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,112,113,114,115,116,117,118,120,121,122,123,124,125,126,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,150,151,152,153,154,155,156,157,158,159,160,161,163,164,165,166,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,210,211,212,213,214,215,216,217,218,219,231,232,233,234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255,256,257,259,260,261,262,263,275,276,277,278,279,280,281,282,283,284,285,286,287,288}
local zombieSkins = {69,70,127,162,167,209,230,258,264}

--Start
function startujemy (res)
	if res == getThisResource() then
		call(getResourceFromName("scoreboard"), "addScoreboardColumn", "Scores")
		ludzie = createTeam ( "Humans", 0, 255, 0)
		zombies = createTeam ( "Zombies", 255, 0, 0)
		setTimer(setGameType,5000,1,"Zombie Infection 0.1" )
		setTeamFriendlyFire(ludzie,false)
		setTeamFriendlyFire(zombies,false)
	for k,v in ipairs(getElementsByType ( "player" )) do
	setElementData(v,"Scores",0)
	end
	end
end

addEventHandler("onGamemodeMapStart", getRootElement(), function () setTimer(nowaRunda,1000,1) end)
addEventHandler ( "onResourceStart", getRootElement(), startujemy )

-- Spawny
function playerspawn(thisplayer)
	currentmap = call(getResourceFromName("mapmanager"),"getRunningGamemodeMap")
	fadeCamera(thisplayer,true)
	local mapinterior = get(getResourceName(currentmap)..".#interior")
	if mapinterior == false then 
		mapinterior = 0
	end
	spawns = getElementByID("spawns")
	local points
	if spawns then
		points = getElementsByType ( "spawnpoint", spawns )
	else
		points = getElementsByType ( "spawns", mapRoot or getRootElement() )
	end
	local random = math.random ( 1, table.getn ( points ) )
	local posX = getElementData(points[random], "posX") 
	local posY = getElementData(points[random], "posY") 
	local posZ = getElementData(points[random], "posZ")
	local rot = getElementData(points[random], "rot") or getElementData(points[random], "rotZ") or 0
	if getPlayerTeam(thisplayer) == ludzie then
		skin = tonumber( humanSkins[math.random(1, table.getn (humanSkins))])
		--setElementData(thisplayer,"health",100)
		setTimer(dajWeapons,500,1,thisplayer)
	elseif getPlayerTeam(thisplayer) == zombies then
		skin = tonumber( zombieSkins[math.random(1, table.getn (zombieSkins))])	
		--setElementData(thisplayer,"health",500)
		toggleControl ( thisplayer, "next_weapon", false )
		toggleControl ( thisplayer, "previous_weapon", false )
	else return end
	spawnPlayer ( thisplayer, posX, posY, posZ, rot, skin, mapinterior )
	--setCameraMode ( thisplayer, "player" )
	setCameraTarget ( thisplayer, thisplayer )
	setElementData ( thisplayer, "waitingtospawn", "nope" )
	fadeCamera(thisplayer,true)
	setTimer(giveWeapon,500,1,thisplayer, 4, 1, true )
end

function player_join () --koles wbil
	setTimer(playerspawn,2000,1,source)
	setPlayerTeam(source,zombies)
	setElementData(source,"Scores", 0 )
	if getPlayerCount() == 1 or countPlayersInTeam ( ludzie ) == 0 then
	outputChatBox("Not enought players restarting round",source)
	setTimer(nowaRunda,1000,1)
	end
end
addEventHandler ( "onPlayerJoin", getRootElement(), player_join )

--Rundy
function nowaRunda()
	if getPlayerCount() == 0 then return end
	if miszionTimer then
	destroyElement(miszionTimer)
	end
	miszionTimer = exports.missiontimer:createMissionTimer (320000,true,true,0.5,20,true,"default-bold",1)
    addEventHandler ( "onMissionTimerElapsed", miszionTimer, onTimeElapsed )
	setTimer(function()for k,v in ipairs(getElementsByType ( "player" )) do setPlayerTeam(v,ludzie) playerspawn(v) end end,2000,1)
	setTimer(losujZombies,20000,1)
end

function onTimeElapsed (who)
setTimer(nowaRunda,1000,1)
if who then
triggerClientEvent(getRootElement(),"grajSound",getRootElement(),"Zombie_win-0"..math.random(1,2)..".ogg")
triggerClientEvent(getRootElement(),"pokazWinLogo",getRootElement(),1)
else
triggerClientEvent(getRootElement(),"grajSound",getRootElement(),"Human_win-0"..math.random(1,2)..".ogg")
triggerClientEvent(getRootElement(),"pokazWinLogo",getRootElement(),2)
end
end

function losujZombies()
local zombiak = getRandomPlayer()
outputChatBox(getPlayerName(zombiak).." is infected!",getRootElement(),255,0,0)
przemienZombie(zombiak)
end

--Zmiana w zombie
function playerDamage ( attacker, weapon, bodypart, loss ) --when a player is damaged
	if getPlayerTeam(source) == ludzie and getPlayerTeam(attacker) == zombies then
		przemienZombie(source)
		local score = getElementData(attacker,"Scores")+1
		setElementData(attacker,"Scores",score)
		exports.killmessages:outputMessage ({getPlayerName(attacker),{"color",255,0,0},{"padding",width=3},{"icon",id=255},{"padding",width=3},getPlayerName(source) },getRootElement(),255,0,0 )
		outputConsole("* "..getPlayerName(attacker).." infected ".. getPlayerName(source))
		if countPlayersInTeam ( ludzie ) == 0 then
			onTimeElapsed (1)
		end
	end	
end
		
addEventHandler ( "onPlayerDamage", getRootElement (), playerDamage )

function przemienZombie (thisplayer)
setPlayerTeam ( thisplayer,zombies)
setElementModel ( thisplayer,tonumber( zombieSkins[math.random(1, table.getn (zombieSkins))]))
triggerClientEvent(getRootElement(),"grajSoundElement",getRootElement(),"zombie_infect_"..math.random(1,3)..".ogg",thisplayer)
setElementData ( thisplayer,"health",500)
toggleControl ( thisplayer, "next_weapon", false )
toggleControl ( thisplayer, "previous_weapon", false )
takeAllWeapons ( thisplayer )
setTimer(giveWeapon,500,1,thisplayer, 4, 1, true )
end

function player_Wasted ( ammo, attacker, weapon, bodypart ) --respawn
	setTimer(playerspawn,2000,1,source)
	if getPlayerTeam(source) == ludzie then
		setPlayerTeam(source,zombies)
	elseif getPlayerTeam(source) == zombies then
		triggerClientEvent(getRootElement(),"grajSoundElement",getRootElement(),"zombie_die"..math.random(1,3)..".ogg",source)
	end
	if attacker then
	local score = getElementData(attacker,"Scores")+1
	setElementData(attacker,"Scores",score)
	end
	if countPlayersInTeam ( ludzie ) == 0 then
			onTimeElapsed (1)
	end	
end
addEventHandler ( "onPlayerWasted", getRootElement(), player_Wasted )

--Pukawki
function dajWeapons (thisplayer)
giveWeapon ( thisplayer, 24 , 1000, true )
giveWeapon ( thisplayer, math.random(30,31) , 2000, false )
giveWeapon ( thisplayer, 16 , 2, false )
giveWeapon ( thisplayer, 4, 1, true )
setTimer(ustawSkille,1000,1,thisplayer)
toggleControl ( thisplayer, "next_weapon", true )
toggleControl ( thisplayer, "previous_weapon", true )
end

function ustawSkille (thisplayer)
setPedStat ( thisplayer, 69, 999 )
setPedStat ( thisplayer, 70, 999 )
setPedStat ( thisplayer, 71, 999 )
setPedStat ( thisplayer, 72, 999 )
setPedStat ( thisplayer, 73, 999 )
setPedStat ( thisplayer, 74, 999 )
setPedStat ( thisplayer, 75, 999 )
setPedStat ( thisplayer, 76, 999 )
setPedStat ( thisplayer, 77, 999 )
setPedStat ( thisplayer, 78, 999 )
setPedStat ( thisplayer, 79, 999 )
setPedStat ( thisplayer, 80, 999 )
setPedStat ( thisplayer, 81, 999 )
end
